package com.erlang4j.internal.basicIo;

import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpPeer;
import com.ericsson.otp.erlang.OtpSelf;
import com.erlang4j.api.exceptions.Erlang4jCannotConnectToVmException;

/** This makes {@link IBasicMailBox} that are {@link OtpSelfBasicMailBox} */
public class OtpSelfBasicMailBoxFactory implements IBasicMailBoxFactory {

	private OtpSelfBasicMailBox otpSelfBasicOperations;

	public OtpSelfBasicMailBoxFactory(String erlangVm, String cookie, String javaVm) {
		OtpSelf self;
		try {
			self = new OtpSelf(javaVm, cookie);
			OtpPeer peer = new OtpPeer(erlangVm);
			OtpConnection connection = self.connect(peer);
			otpSelfBasicOperations = new OtpSelfBasicMailBox(connection);
		} catch (Exception e) {
			throw new Erlang4jCannotConnectToVmException(javaVm, erlangVm, cookie, e);
		}
	}

	@Override
	public IBasicMailBox operations() {
		return otpSelfBasicOperations;
	}

}
